<template>
  <div class="app-container">
    <el-card class="mar_bottom">
      <el-search-form label-width="90px" @search="onSearch" @reset="onReset">
        <el-form-item>
          <el-select
            size="small"
            style="width: 100px; margin-right: -2px"
            v-model="timeType"
            placeholder="请选择"
          >
            <el-option label="创建时间" value="createDate"></el-option>
            <el-option label="订单时间" value="orderTime"></el-option>
          </el-select>
          <el-date-picker
            size="small"
            v-model="searchBody.timeRange"
            style="width: 350px"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>


        <el-form-item label="账号">
          <el-input
            clearable
            size="small"
            v-model="searchBody.phone"
            class="el-input__small"
          ></el-input>
        </el-form-item>

        <el-form-item label="姓名">
          <el-input
            clearable
            size="small"
            v-model="searchBody.userName"
            class="el-input__small"
          ></el-input>
        </el-form-item>


      </el-search-form>

      <el-button type="primary"
                 size="small"
                 @click="handleClick('exportOut')"
      >导出数据</el-button>
      <el-upload
        style="display: inline-block"
        ref="uploadJiesuan"
        :show-file-list="false"
        action
        accept=".xlsx"
        :http-request="uploadData"
        :before-upload="beforeUpload"
      >
        <el-button type="primary" style="margin-left: 10px" size="small"
        >导入转账记录</el-button
        >
      </el-upload>
      <el-button :type="this.tixianConfigSet === '1' ? 'primary' : 'danger'"
                 size="small"
                 @click="handleClick('tixianConfigSet')"
      >{{this.tixianConfigSet === '1' ? '暂停提现' : '恢复提现'}}</el-button>
    </el-card>

    <el-card>
      <div>
        <el-tag type="info">提现总金额 : {{totalTixianPrice}}</el-tag>
         -
        <el-tag type="warning">手续费总金额 : {{totalShoufufei}}</el-tag>
         =
        <el-tag >到账总金额 : {{totalDaozhang}}</el-tag>
      </div>

      <el-tabs
        style="margin-top: 15px"
        v-model="searchBody.tixianStatus"
        @tab-click="getData"
      >
        <el-tab-pane label="全部" name="0"></el-tab-pane>
        <el-tab-pane label="已提交未到账" name="1"></el-tab-pane>
        <el-tab-pane label="提现失败" name="2"></el-tab-pane>
        <el-tab-pane label="提现成功" name="5"></el-tab-pane>
        <el-tab-pane label="取消提现" name="3"></el-tab-pane>

      </el-tabs>

      <el-pagination
        small
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="pageSizeOpts"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
      <div class="have-pagination-container">
        <el-table v-loading="loading.list" :data="list"  >
          <el-table-column
            label="id"
            prop="id"
            align="center"
            width="50"
          ></el-table-column>
          <el-table-column
            label="提现时间"
            prop="tixianTime"
            align="center"
            width="150"
          ></el-table-column>
          <el-table-column
            label="手机号(账户)"
            prop="phone"
            align="center"
            width="100"
          ></el-table-column>
          <el-table-column
            label="姓名"
            prop="userName"
            align="center"
            width="100"
          ></el-table-column>
          <el-table-column
            label="提现金额"
            prop="tixianPrice"
            align="center"
            width="100"
            sortable
          >
            <template slot-scope="scope">
              <el-tag
                type="info"
                disable-transitions>{{scope.row.tixianPrice}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="手续费"
            prop="shoufufei"
            align="center"
            width="100"
            sortable
          >
            <template slot-scope="scope">
              <el-tag
                type="warning"
                disable-transitions>{{scope.row.shoufufei}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="到账金额"
            prop="daozhangPrice"
            align="center"
            width="100"
            sortable
          >
            <template slot-scope="scope">
              <el-tag
                disable-transitions>{{scope.row.daozhangPrice}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="银行"
            prop="tixianBankName"
            align="center"
            width="160"
          ></el-table-column>
          <el-table-column
            label="支行"
            prop="tixianBankZhihang"
            align="center"
            width="400"
          ></el-table-column>
          <el-table-column
            label="卡号"
            prop="tixianBankNo"
            align="center"
            width="260"
          ></el-table-column>
          <el-table-column
            label="银行绑定手机"
            prop="tixianBankPhone"
            align="center"
            width="100"
          ></el-table-column>
          <el-table-column
            label="余额"
            prop="balance"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column
            label="提现状态"
            prop="tixianStatusValue"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.tixianStatus === '1' ? 'info' : scope.row.tixianStatus === '2' ? 'danger' : scope.row.tixianStatus === '3' ? 'warning' : scope.row.tixianStatus === '5' ? 'success' : 'info'"
                disable-transitions>{{scope.row.tixianStatusValue}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="详情"
            prop="remark"
            align="center"
            width="360"
          ></el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="160"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                @click="handleClick('cancelTixian', scope.row)"
                :disabled="scope.row.tixianStatus !='1'"
              >撤销提现</el-button>
              <el-button
                type="primary"
                size="small"
                @click="handleClick('tixianLogSyncUserBank', scope.row)"
                :disabled="scope.row.tixianStatus !=='1'"
              >同步用户卡号信息</el-button>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <el-pagination
        small
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="pageSizeOpts"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </el-card>
    <!-- 撤销 提现弹窗 -->
    <el-dialog
      :title="cancelTixianTitle"
      :visible.sync="cancelTixianDialog"
      width="45%"
      :close-on-click-modal="false"
    >

          <el-form
            label-width="100px"
            ref="updateForm2"
            inline
            :model="updateForm"
          >

            <el-form-item label="原因" prop="remark">
              <el-input
                clearable
                size="small"
                v-model="updateForm.remark"
                class="el-input__small"
              ></el-input>
            </el-form-item>

          </el-form>



      <span slot="footer" class="dialog-footer">
        <el-button type="primary" plain @click="cancelTixianDialog = false"
        >取 消</el-button
        >
        <el-button type="primary" @click="handleSure('cancelTixian')"
        >确 定</el-button
        >
      </span>
    </el-dialog>


  </div>
</template>

<script>
import {
  paidanOrder,
  selectPaidanPageVo,
  selectRegionTree,
  sysDictbyMultiType,
  addNewOrder,
  editXiansuo,
  exportInData,
  exportOutData,
  selectRegisterListPage,
  jiazhuangdingdanshenhe,
  updateUserLevel,
  selectShouyiPageByOrder,
  selectShouYiPageByUser,
  selectHoutaiUserTixianLogPage,
  exportTixianLog,
  exportJiazhuangOrderList,
  exportInZhuanzhangLog,
  selectTotalOrderPrice,
  tixianLogSyncUserBank,
  cancelTixianLog,
  selectHoutaiUserTixianLogTotalPrice,
  selectSysConfigVo,
  setCanTixianConfig,
} from "@/api/api.js";
import {sysDict_delete} from "@/api/dataDict";
import {downloadFile} from "@/utils";
import {sysrole_delete} from "@/api/platformSys";

const getDefaultForm = () => ({
  id:null,
  userName:null,
  tixianBankName:null,
  tixianBankZhihang:null,
  tixianBankNo:null,
  tixianPrice:null,
  remark:null,
});
const getDefaultSearchBody = () => ({
  timeRange: null,
  resource: null,
  phone: null,
  yezhu: null,
  orderNo: null,
  userIdList: null,
  id: null,
  audiStatus: "0",
  levelId:null,
  tixianStatus:"0",
  userName:null,
});
export default {
  data() {
    return {
      cancelTixianDialog:false,
      cancelTixianLoading: false,
      cancelTixianTitle:null,
      tixianConfigSet:"1",
      orderRiZhi: [],
      areaOptions: [],
      qdzOptions: [],
      resourceOptions: [],
      fw1Options: [],
      fw2Options: [],
      zxfs1Options: [],
      zxfs2Options: [],
      zxfs3Options: [],
      zxfs4Options: [],
      xhfgOptions: [],
      zxysOptions: [],
      jsStatusOptions: [],
      timeType: "createDate",
      searchType: "phone",
      dialogTitle: "",
      dialogTitle2: "",
      dialogTitle3: "",
      handleType: "",
      pageNo: 1,
      pageSize: 20,
      totalCount: 0,
      childPageNo: 1,
      childPageSize: 20,
      childTotalCount: 0,
      pageSizeOpts: [20, 50, 100, 300, 500],
      loading: {
        list: false,
        list2: false,
        childList: false,
      },
      list: [],
      paidanData: {},
      selectRow: [], // 选中行
      lookDialog: false,
      editDialog: false,
      paidanDialog: false,
      updateForm: getDefaultForm(),
      searchBody: getDefaultSearchBody(),
      configDialog:false,
      totalTixianPrice: 0,
      totalDaozhang: 0,
      totalShoufufei: 0,

      tableColumn: [
        {
          label: "线索ID",
          checked: true,
        },
        {
          label: "线索业主",
          checked: true,
        },
        {
          label: "线索手机",
          checked: true,
        },
        {
          label: "线索城市",
          checked: true,
        },
        {
          label: "派单",
          checked: true,
        },
        {
          label: "接单",
          checked: true,
        },
        {
          label: "订单",
          checked: true,
        },
        {
          label: "结算",
          checked: true,
        },
        {
          label: "标记",
          checked: true,
        },
        {
          label: "更新小计",
          checked: true,
        },
        {
          label: "来源",
          checked: true,
        }
       /* {
          label: "管理员/渠道主/达人",
          checked: true,
        },*/
        /*{
          label: "手机归属",
          checked: true,
        },
        {
          label: "线索时间",
          checked: true,
        },
        {
          label: "需求时间（订单）",
          checked: true,
        },
        {
          label: "更新时间",
          checked: true,
        },
        {
          label: "更新人",
          checked: true,
        },*/
      ],
      tableColumn2: [
        {
          label: "线索ID",
          checked: true,
        },
        {
          label: "订单GUID",
          checked: false,
        },
        {
          label: "线索UUID",
          checked: true,
        },
        {
          label: "线索姓名",
          checked: true,
        },
        {
          label: "线索城市",
          checked: true,
        },
        {
          label: "线索时间",
          checked: true,
        },
        {
          label: "渠道主",
          checked: true,
        },
        {
          label: "达人",
          checked: true,
        },
        {
          label: "派单时间",
          checked: true,
        },
        {
          label: "接单时间",
          checked: true,
        },
        {
          label: "产生需求",
          checked: true,
        },
        {
          label: "需求时间(订单)",
          checked: true,
        },
        {
          label: "需求城市",
          checked: true,
        },
        {
          label: "渠道主奖励",
          checked: true,
        },
        {
          label: "达人奖励",
          checked: true,
        },
        {
          label: "结算状态",
          checked: true,
        },
        {
          label: "结算时间",
          checked: true,
        },
      ],
      shouyiUserId: 0,
      shouyiDialog: false,
    };
  },
  created() {
    this.getData();
    //this.getQdzOptions();
    //this.getOptions();
  },
  methods: {
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    /*getOptions() {
      sysDictbyMultiType({
        dictType:
          "resource,fangwugaikuan1,fangwugaikuan2,zhuangxiufangshi1,zhuangxiufangshi2,zhuangxiufangshi3,zhuangxiufangshi4,xihuanfengge,zhuangxiuyusuan,settlestatus",
      }).then((res) => {
        this.resourceOptions = res.result.resource || [];
        this.fw1Options = res.result.fangwugaikuan1 || [];
        this.fw2Options = res.result.fangwugaikuan2 || [];
        this.zxfs1Options = res.result.zhuangxiufangshi1 || [];
        this.zxfs2Options = res.result.zhuangxiufangshi2 || [];
        this.zxfs3Options = res.result.zhuangxiufangshi3 || [];
        this.zxfs4Options = res.result.zhuangxiufangshi4 || [];
        this.xhfgOptions = res.result.xihuanfengge || [];
        this.zxysOptions = res.result.zhuangxiuyusuan || [];
        this.jsStatusOptions = res.result.settlestatus || [];
      });
      selectRegionTree().then((res) => {
        this.areaOptions = res.result || [];
      });
    },*/
   /* getQdzOptions() {
      selectQuDaoDarenTree().then((res) => {
        this.qdzOptions = res.result || [];
      });
    },*/
    onSearchTypeChange(key) {
      let val;
      for (let key2 of ["yezhu", "phone", "orderNo", "id"]) {
        val = this.searchBody[key2] || val;
      }
      this.searchBody = Object.assign(this.searchBody, {
        [key]: val,
      });
    },
    getData() {
      this.loading.list = true;
      const { timeRange, userIdList, ...rest } = this.searchBody;
      const [startTime, endTime] = timeRange || [null, null];
      /*const _userIdList = (userIdList || []).reduce((a, b) => {
        return Array.from(new Set(a.concat(b)));
      }, []);*/
      selectHoutaiUserTixianLogPage({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        startTime: startTime,
        endTime: endTime,

        ...rest,
      }).then(({ result }) => {
        this.list = result.data;
        this.totalCount = result.totalCount;
        this.loading.list = false;
      });
      selectHoutaiUserTixianLogTotalPrice({

        startTime: startTime,
        endTime: endTime,

        ...rest,
      }).then(({ result }) => {
        this.totalTixianPrice = result.tixianPrice;
        this.totalDaozhang = result.daozhangPrice;
        this.totalShoufufei = result.shoufufei;
      });

      //系统配置查看
      selectSysConfigVo().then(({ result }) => {
        this.tixianConfigSet = result.canTixian;
      });
    },
    handleCurrentChangeChild(type, val) {
      switch (type) {
        case "list":
          this.pageNo = val;
          this.getData();
          break;
        case "childList":
          this.childPageNo = val;
          this.getShouyiData();
          break;
      }
    },
    handleSizeChangeChild(type, val) {
      switch (type) {
        case "list":
          this.pageSize = val;
          this.getData();
          break;
        case "childList":
          this.childPageSize = val;
          this.getShouyiData();
          break;
      }
    },
    getData2() {},
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    shouyiTableRowClassName({row, rowIndex}){
      if (row.levelValue === '黄金') {
        return 'huangjing-row';
      }
      return '';

    },
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getData();
    },
    handleSelectionChange(val) {
      this.selectIds = val.map((d) => d.id);
    },
    onSearch() {
      this.pageNo = 1;
      this.getData();
    },
    onReset() {
      this.pageNo = 1;
      this.searchBody = getDefaultSearchBody();
      this.getData();
    },
    getShouyiData() {
      this.loading.childList = true;
      selectShouYiPageByUser({
        pageNo: this.childPageNo,
        pageSize: this.childPageSize,
        userId: this.shouyiUserId,
      })
        .then(({ result = {} }) => {
          this.childList = result.data;
          this.childTotalCount = result.totalCount;
          this.loading.childList = false;
        })
        .catch(() => {
          this.loading.childList = false;
        });
    },
    handleClick(type, row, title) {
      this.handleType = type;
      switch (type) {
        case "tixianConfigSet":
          var msg1 =this.tixianConfigSet === '1' ? '暂停提现' : '恢复提现';
          this.$confirm(`确认？`+msg1, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            setCanTixianConfig({
              }).then((res) => {
              this.$message.success(res.msg || "操作成功");
              this.getData();
            });
          });
          break;
        case "tixianLogSyncUserBank":
          this.$confirm(`确认同步银行信息？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            tixianLogSyncUserBank({
              id: row.id,
            }).then((res) => {
              this.$message.success(res.msg || "操作成功");
              this.getData();
            });
          });
          break;
        case "cancelTixian":
          this.cancelTixianDialog = true;
          this.cancelTixianTitle = "撤销体现 ："+row.phone + " 金额 ： "+row.tixianPrice;
          this.updateForm.id = row.id;
          break;
        case "exportOut":
          const { timeRange, userIdList, ...rest } = this.searchBody;
          const [startTime, endTime] = timeRange || [null, null];
          exportTixianLog(
            {
              startTime: startTime,
              endTime: endTime,
              phone:this.searchBody.phone,
              tixianStatus:this.searchBody.tixianStatus
            }
          ).then((res) => {
            let fileName = res.headers["content-disposition"].replaceAll(
              /.*?filename=(.*?)/g,
              "$1"
            );
            downloadFile(res.data, decodeURI(fileName), "blob");
            this.$message.success("导出成功");
          });
          break;
        case "exportInZhuanzhangLog":

          break;
        case "exportOut":
          exportOutData().then((res) => {
            this.$message.success("导出成功");
          });
          break;
        case "paidan":
          this.dialogTitle3 = "订单ID:" + row.id + "-派单";
          selectPaidanPageVo({
            id: row.id,
          }).then((res) => {
            const paidanData = res.result || {};
            paidanData.orderId = row.id;
            this.paidanData = paidanData;
            this.paidanDialog = true;
          });
          break;
        case "shenhe":
          this.$confirm(`确认审核通过？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            jiazhuangdingdanshenhe({
              id: row.id,
            }).then((res) => {
              this.$message.success(res.msg || "操作成功");
              this.getData();
            });
          });
          break;
        case "selectShouyi":
          this.shouyiDialog = true;
          this.shouyiUserId = row.id;
          this.getShouyiData();
          break;
        default:
          return;
      }
    },
    handleSure(type) {
      console.log(type);
      type = type || this.handleType;
      //const { addrCode, ...rest } = this.updateForm;
      switch (type) {

        case "cancelTixian":
          this.$confirm(`确认撤销提现？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            cancelTixianLog({
                id:this.updateForm.id,
                remark:this.updateForm.remark,
              }
            ).then((res) => {
              this.$message.success(res.msg || "操作成功");
              this.getData();
            });
          });
          break
        case "paidan":
          paidanOrder({
            orderId: this.paidanData.orderId,
            jiazhuangPlatformIdList: this.selectRow.map((d) => {
              return d.jiazhuangPlatformId;
            }),
          }).then((res) => {
            this.$message.success("派单成功");
            this.paidanDialog = false;
          });
          break;
        default:
          return;
      }
    },
    onSelectionChange(val) {
      this.selectRow = val;
    },
    handleCommand(command) {
      this.$message("click on item " + command);
    },
    uploadData(params) {
      const form = new FormData();
      form.append("file", params.file);
      const loading = this.$loading({
        lock: true,
        text: "正在导入...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      exportInZhuanzhangLog(form)
        .then((res) => {
          this.$message.success("导入成功");
          loading.close();
        })
        .catch(() => {
          loading.close();
        });
    },
    beforeUpload(file) {
      if (file.size > 5 * 1024 * 1024) {
        this.$message.warning("上传的文件大小不能超过5MB!");
        return false;
      }
    },
  },
};
</script>

<style>
.power_right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 30px;
  margin-bottom: 10px;
}

.block {
  display: flex;
  height: 70px;
}
.block_item {
  padding: 5px 30px;
  box-sizing: border-box;
  border-right: 1px solid #ddd;
  color: #5f5f5f;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
.block_item:first-of-type {
  border-right: 0;
  align-self: flex-start;
}
.value {
  font-size: 24px;
  font-weight: bold;
}
.baijin-row {
  background: #fffbec;
}

.huangjing-row {
  background: #fff740;
}
.zuanshi-row {
  background: #95ffff;
}
.el-table .warning-row {
  background: #95ffff;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
